function get_fb_friends_callback(response)
{
    if (response.error) {
	alert("get friends list from facebook error.");
	return false;
    }
    var data = {
	fb_friends_info : response,	
    };    
    $.ajax({
	'type' : 'POST',
	'data' : data,
	'dataType' : 'json',
	'url'  : '/user/ajaxUploadFbFriendList',
	success : function(msg){
	    if (msg.status == 0) {
		var strFriendListHTML = "";
		var strFriendItem = "";
		for (var i = 0; i < msg.content.length; ++i) {
		    strFriendItem = "<li id='"+msg.content[i].id+"'><img src='https://graph.facebook.com/"+msg.content[i].id+"/picture' /><p class='name'>"+msg.content[i].name+"</p><p class='oper'><a href='#' class='invite' user_id='"+msg.content[i].id+"'></a><a href='#' class='skip' user_id='"+msg.content[i].id+"'>skip</a></p><p class='from'>via wall post</p></li>"; 
		    strFriendListHTML += strFriendItem;
		}
		$("#invite_friend_list").html(strFriendListHTML);
		$(".skip").click(skip_fb_friend);
		$(".invite").click(invite_fb_friend);
	    }
	    else {
		alert(msg.content);
	    }

	},
	error : function(xhr) {
	    alert("Exception");
	}
    });
}

var item;
function skip_fb_friend()
{
    item = this;
    $.ajax({
	'type' : 'GET',
	'dataType' : 'json',
	'url' : '/user/ajaxskipfbfriend',
	success : function(msg) {
	    strNewHTML = "<img src='https://graph.facebook.com/"+msg.content.id+"/picture' /><p class='name'>"+msg.content.name+"</p><p class='oper'><a href='#' class='invite' user_id='"+msg.content.id+"'></a><a href='#' class='skip' user_id='"+msg.content.id+"'>skip</a></p><p class='from'>via wall post</p>";
	    $(item).parent().parent().html(strNewHTML);
	    $(".skip").click(skip_fb_friend);
	    $(".invite").click(invite_fb_friend);

	},
	error:  function(xhr) {
	    alert("Exception");	
	}
    }); 
    return false;
}

function invite_fb_friend_callback(response)
{
    alert("Invite Success.");
    var id = response.id;
    var uid = id.split("_")[0];
    var data = {
	fb_uid : uid
    };
    //1. set the uid is invited.
    $.ajax({
	'type' : 'GET',
	'dataType' : 'json',
	'data' : data,
	'url' : '/user/ajaxinvitefbfriend',
	success : function(msg) {
	    if (msg.status == 0) {
		$.ajax({
		    'type' : 'GET',
		    'dataType' : 'json',
		    'url' : '/user/ajaxskipfbfriend',
		    success : function(msg) {
			strNewHTML = "<img src='https://graph.facebook.com/"+msg.content.id+"/picture' /><p class='name'>"+msg.content.name+"</p><p class='oper'><a href='#' class='invite' user_id='"+msg.content.id+"'></a><a href='#' class='skip' user_id='"+msg.content.id+"'>skip</a></p><p class='from'>via wall post</p>";
			$(item).parent().parent().html(strNewHTML);
			$(".skip").click(skip_fb_friend);
			$(".invite").click(invite_fb_friend);

		    },
		    error:  function(xhr) {
			alert("Exception");	
		    }
		}); 
		
	    }
	    else {
		alert(msg.content);
	    }
	},
	error:  function(xhr) {
	    alert("Exception");	
	}
    }); 

    //get another friend
}
function invite_fb_friend()
{
    var id = $(this).attr("user_id");
    var username = $(".user-nickname").text();

    var content = username + " invites you to share your fitness goal, get free personal training on Fitwow.com";
    var msg = {
	message:content,
	name : "Invite to join",
	link : "http://fitwow.com"
    };
    fb_publish_feed(id, msg, invite_fb_friend_callback);
    item = this;
    return false;
}

//insert goal
function insert_goal_list(id, data)
{
    var strHTML = "";
    for(var i = 0; i < data.length; ++i) {
	if (i % 2 == 0) {
	    strHTML += '<tr class="bg"><td class="w235"><a href="/goal/detail/gid/'+data[i].id+'">'+data[i].goal_name+'</a></td><td>'+data[i].created_time+'</td><td>'+data[i].bid_cnt+'</td><td>'+data[i].cheerleader_cnt+'</td><td><a href="/goal/detail/gid/'+data[i].id+'"><span>'+data[i].status+'</span></a></td></tr>';		
	}
	else {
	    strHTML += '<tr><td class="w235"><a href="/goal/detail/gid/'+data[i].id+'">'+data[i].goal_name+'</a></td><td>'+data[i].created_time+'</td><td>'+data[i].bid_cnt+'</td><td>'+data[i].cheerleader_cnt+'</td><td><a href="/goal/detail/gid/'+data[i].id+'"><span>'+data[i].status+'</span></a></td></tr>';		
	}
    }
    $("#"+id).html(strHTML);
}

function insert_friends_goal_list(id, data)
{
    var strHTML = "";
    for(var i = 0; i < data.length; ++i) {
	strHTML += '<tr class="top"><td class="w98" rowspan="2"><img src="/'+data[i].user.thumb_photo+'" /></td><td colspan="7" class="list-text"><a href="/goal/detail/gid/'+data[i].id+'"><strong>'+data[i].goal_name+'</strong></a></td></tr>';

	strHTML += '<tr class="list-body"><td class="list-text w150"><p>Posted on '+data[i].created_time+'<br />'+data[i].area+'</p></td><td class="w50">$'+data[i].budget+'</td><!--<td class="w100">'+data[i].deadline+'</td>--><td class="w100">'+data[i].created_time+'</td><td class="w50">'+data[i].bid_cnt+'</td><td class="w100">'+data[i].cheerleader_cnt+'</td><td>'+data[i].status+'</td></tr>';

	strHTML += '<tr class="line"><td class="list-name w98"><a href="/user/detail/uid/'+data[i].user.id+'" class="name">'+data[i].user.firstname+'</a></td><td colspan="7"><label class="cheer"><a href="#pop4"class="show_pop4" id="'+data[i].id+'" goal_name="'+data[i].goal_name+'">Cheer me on</a></label><span>'+data[i].cheerleader_cnt+' cheerleaders</span><a href="/goal/community/gid/'+data[i].id+'">Community</a></td></tr>';
    }
    $("#"+id).html(strHTML);
    $('.show_pop4').fancybox({	
	'width': '75%',
	'height': '75%',	
	'autoScale'	: false,
	'transitionIn': 'none',
	'transitionOut': 'none'
	}).click(function(){ 
	    $("#gid").val($(this).attr("id"));
	    var goal_name = $(this).attr("goal_name");
	    goal_name = "Goal: " + goal_name;
	    $("#h1_goal_name").text(goal_name); 
	});
}

function change_pagelist(id, page)
{
    var idx = 0;
    $("#"+id).find("li").each(function(){
	if ($(this).attr("class") == "last-page") {
	    if (page == 1) {
		$(this).children().each(function(){$(this).attr("id", 1)});
	    }
	    else {
		$(this).children().each(function(){$(this).attr("id", page-1)});
	    }
	    ++idx;
	    return;
	}
	if ($(this).attr("class") == "next-page") {
	    if (page == 10) {
		$(this).children().each(function(){$(this).attr("id", 10)});
	    }
	    else {
		$(this).children().each(function(){$(this).attr("id", page+1)});
	    }
	    ++idx;
	    return;
	     
	}
	if (idx == page) {
	    $(this).attr("class", "now");
	}    
	else {
	    $(this).removeClass();
	}
	++idx;
    });
}
function insert_bid_list(id, data)
{
    var strHTML = '<tr><th class="w285">Bid Name</th><th >Date submitted</th><th class="w94">Prize</th><th>Next step</th></tr>';
    for(var i = 0; i < data.length; ++i) {
	if (i %2 == 0) {
	    strHTML += '<tr class="bg"><td class="w285"><a href="/goal/biddetail/bid/'+data[i].id+'">'+data[i].subject+'</a></td><td>'+data[i].created_time+'</td><td>$'+data[i].bid_price+'</td><td><a href="/goal/biddetail/bid/'+data[i].id+'"><span>'+data[i].status+'</span></a></td></tr>';
	}
	else {
	    strHTML += '<tr ><td class="w285"><a href="/goal/biddetail/bid/'+data[i].id+'">'+data[i].subject+'</a></td><td>'+data[i].created_time+'</td><td>$'+data[i].bid_price+'</td><td><a href="/goal/biddetail/bid/'+data[i].id+'"><span>'+data[i].goal.status+'</span></a></td></tr>';
	    
	}
    }
    $("#"+id).html(strHTML);
}

function insert_review_list(id, data)
{
    var strHTML = '';
    for (var i = 0; i < data.length; ++i) {
	strHTML += '<tr class="ask-box"><td class="left-say"> <a href="/user/detail/uid/'+data[i].reviewer_uid+'"> <img src="/'+data[i].user.thumb_photo+'" /> <span>'+data[i].user.firstname+'</span></a></td>';

        strHTML += '<td class="right-say"><div class="saybox"><s></s><p>'+data[i].content+'</p><span>'+data[i].created_time+'<label class="star s_5"></label></span></div></td></tr>';
    }
    $("#"+id).html(strHTML);
}

function paging_func()
{
    var page = $(this).attr("id");
    page = parseInt(page);
    var act  = $(this).attr("act");
    var uid  = $(this).attr("uid");
    var data = {
	page : page,
	act  : act,
	uid  : uid
    }
    $.ajax({
	'type' : 'POST',
	'data' : data,
	'dataType' : 'json',
	'url'  : '/user/ajaxloaddata',
	success : function(msg){
	    if (msg.status == 0) {
		if (act == "my_goal") {
		    insert_goal_list("my_goals_list", msg.data_list);
		    change_pagelist("my_goals_page", page);
		}
		else if (act == 'friends_goal'){
		    insert_friends_goal_list("my_friends_goals_list", msg.data_list);
		    change_pagelist("my_friends_goals_page", page);
		}
		else if (act == 'bid') {
		    insert_bid_list("my_bids", msg.data_list);
		    change_pagelist("my_bids_page", page);
		    
		}
		else if (act == 'review') {
		    insert_review_list("my_reviews", msg.data_list);
		    change_pagelist("my_reviews_page", page);
		}
	    }
	    else {
		alert(msg.content);
	    }

	},
	error : function(xhr) {
	    alert("Exception");
	}
    
    });
}

function read_alert()
{
    var nid = $(this).attr("id");
    //alert(nid);
    var data = {
        nid: nid
    };
    
    $.ajax({
	'type' : 'POST',
	'data' : data,
	'dataType' : 'json',
	'url'  : '/user/readalert',
	success : function(msg){
        //alert(msg.content);
	},
	error : function(xhr) {
	    alert("Exception");
	}
    });
    return true;
}
$(function (){
    //fb_init2(document);
    if (is_facebook_user) {
        if (is_upload_fb_friends == 0) {
            fb_get_friendlists(get_fb_friends_callback);
        }
    }
    else {
        //not facebook user
        
    }
    //
    $(".skip").click(skip_fb_friend);
    $(".invite").click(invite_fb_friend);

    //dashbaord goal page list
    $(".paging li a").click(paging_func);
    $("#alert_table").find("a").each(function(){$(this).click(read_alert)})
});
